[VB解题]编写一个程序,求两个正整数的最小公倍数

来源:百度知道 编辑:UC知道 时间:2024/07/03 11:04:32
从两个文本框中输入两个正整数,单击"求解"按钮,在第三个文本框中显示求得的这两个数的最小公倍数
程序中应包含一个过程,用于求两个正整数的最小公倍数

Private Sub command1_click()
Dim n1 As Long, n2 As Long
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
T n1, n2
Text3.Text = n1
End Sub
Private Sub T(n1 As Long, n2 As Long)
Dim i As Long, j As Long
If n1 > n2 Then
j = n1: n1 = n2: n2 = j
End If
For i = 1 To n1
If n2 * i Mod n1 = 0 Then
n1 = n2 * i
Exit Sub
End If
Next i
End Sub

Private Sub Command1_Click()
Dim a As Integer, b As Integer, min As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
If a < 1 Or b < 1 Then MsgBox "请输入2个大于或等于1的整数!": Exit Sub
If a < b Then min = a Else min = b
f a, b, min
End Sub
Function f(a As Integer, b As Integer, min As Integer) As Integer
Dim i As Integer, t As Integer
For i = 1 To min
If a Mod i = 0 And b Mod i = 0 Then t = i
Next i
f = a / t * (b / t) * t
Text3.Text = f
End Function
<